using System;
using System.Collections;
using ManticLabs.Common;

namespace ManticLabs.Data
{
	public class DAArrayList:ArrayList
	{
		private ArrayList fieldAttributes;
        private int fieldCount=0;
        public int FieldCount
        {
            get { return fieldCount; }
            set {fieldCount = value; }
        }
        public DAArrayList():base()
        {
        }

		internal DAArrayList(ICollection c):base(c)
		{
		}
		
		protected DAArrayList(int c):base(c)
		{
		}

		internal void SetFieldAttributes(ArrayList fieldAttributes)
		{
			this.fieldAttributes = fieldAttributes;
		}

        public ArrayList GetFieldAttributes()
        {
            return fieldAttributes;
        }
        
        public object Get(string columnName, int rowIndex)
		{
			if (columnName == null)
				throw new MLabsException("Column Name cannot be null", LogLevelType.ERROR);   
			if(fieldAttributes == null)
				throw new MLabsException("Cannot get field value by column name - set setFieldAttributes parameter to true", LogLevelType.ERROR);   

			try
			{
				object[] row = (object[] )this[rowIndex];

				for(int i=0; i<fieldAttributes.Count; i++)
				{
					FieldAttribute fa = (FieldAttribute )fieldAttributes[i];
                    if (fa.columnName.ToUpper().Equals(columnName.ToUpper()))
                    {
                        return row[i];
                    }
				}

				throw new MLabsException("Cannot get field value by column name. Column: " + columnName + ".", LogLevelType.ERROR);   

			}
			catch(MLabsException ex)
			{
				throw new MLabsException(ex.Message, ex, LogLevelType.ERROR); 
			}
			catch(Exception ex)
			{
				throw new MLabsException(ex.Message, ex, LogLevelType.ERROR); 
			}
		}

        public object Get(int columnIndex, int rowIndex)
		{

			try
			{
				object[] row = (object[] )this[rowIndex];
                return row[columnIndex];

			}
			catch(Exception ex)
			{
				throw new MLabsException(ex.Message, ex, LogLevelType.ERROR); 
			}
		}

		public object Get(string columnName, int rowIndex, string dbNullToString)
		{
			if (columnName == null)
				throw new MLabsException("Column Name cannot be null", LogLevelType.ERROR);   
			if(fieldAttributes == null)
				throw new MLabsException("Cannot get field value by column name - set setFieldAttributes parameter to true", LogLevelType.ERROR);   

			try
			{
				object[] row = (object[] )this[rowIndex];

				for(int i=0; i<fieldAttributes.Count; i++)
				{
					FieldAttribute fa = (FieldAttribute )fieldAttributes[i];
					if (fa.columnName.ToUpper().Equals(columnName.ToUpper()))
					{
						if (Convert.IsDBNull(row[i]))
							return dbNullToString;
						else
							return row[i];
					}
				}

				throw new MLabsException("Cannot get field value by column name. Column: " + columnName + ".", LogLevelType.ERROR);   

			}
			catch(MLabsException ex)
			{
				throw new MLabsException(ex.Message, ex, LogLevelType.ERROR); 
			}
			catch(Exception ex)
			{
				throw new MLabsException(ex.Message, ex, LogLevelType.ERROR); 
			}
		}

		public object Get(int columnIndex, int rowIndex, string dbNullToString)
		{

			try
			{
				object[] row = (object[] )this[rowIndex];

				if (Convert.IsDBNull(row[columnIndex]))
					return dbNullToString;
				else
					return row[columnIndex];

			}
			catch(Exception ex)
			{
				throw new MLabsException(ex.Message, ex, LogLevelType.ERROR); 
			}
		}
	}
}